package main.find;

public class InsertionFind extends BinnaryFind {
    private int value;

    public InsertionFind(int[] array) {
        super(array);
        this.name = "插值查找";
    }

    @Override
    public int find(int value) {
        this.value = value;
        return super.find(array, value, 0, array.length - 1);
    }

    @Override
    public int getMid(int start, int end) {
        start = Math.min(start, array.length - 1);
        end = Math.min(end, array.length - 1);
        if (start == end || array[start] == array[end] || value == array[start]) {
            return start;
        }
        return start + (end - start) * ((value - array[start]) / (array[end] - array[start]));
    }
}
